<template>
	<view style="background-color: #fff;" v-if="goodsInfo.goods_name" class="goods-detail-container">
		<!-- 轮播图区域 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
			<swiper-item v-for="(item, i) in goodsInfo.pics" :key="i">
				<image :src="item.pics_big" @click="preview(i)"></image>
			</swiper-item>
		</swiper>
		<!-- 商品信息区 -->
		<view class="goods-info-box">
			<!-- 商品价格 -->
			<view class="price">￥{{goodsInfo.goods_price}}</view>
			<!-- 信息主体区域 -->
			<view class="goods-info-body">
				<!-- 商品名称 -->
				<view class="goods-name">{{goodsInfo.goods_name}}</view>
				<!-- 收藏 -->
				<view class="favi" @click="addGoodsToStar">
					<!-- 收藏：star,gray; 已收藏：star-filled,orange -->
					<uni-icons :type="goodsInfo.starInfo.type" size="18" :color="goodsInfo.starInfo.color"></uni-icons>
					<text>{{goodsInfo.starInfo.text}}</text>
				</view>
			</view>
			<!-- 运费 -->
			<view class="yf">快递：免运费</view>
		</view>
		<!-- 商品图文数据 -->
		<rich-text :nodes="goodsInfo.goods_introduce"></rich-text>
		<!-- 商品导航组件区 -->
		<view class="goods_nav">
			<uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
				@buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex'
	export default {
		computed: {
			...mapState('m_cart', ['cart', 'star']),
			...mapGetters('m_cart', ['total'])
		},
		watch: {
			total: {
				handler(newVal) {
					const findRes = this.options.find(x => x.text === '购物车')
					if (findRes) {
						findRes.info = newVal
					}
				},
				immediate: true
			}
		},
		data() {
			return {
				// 商品详情
				goodsInfo: {},
				// 左侧按钮配置对象
				options: [{
					icon: 'headphones',
					text: '客服'
				}, {
					icon: 'shop',
					text: '店铺',
				}, {
					icon: 'cart',
					text: '购物车',
					info: 0
				}],
				// 右侧按钮配置对象
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				]
			};
		},
		onLoad(options) {
			// 获取商品 Id
			const goods_id = options.goods_id
			// 调用请求商品详情数据的方法
			this.getGoodsDetail(goods_id)
		},
		methods: {
			...mapMutations('m_cart', ['addToCart', 'addtoStar', 'cancelStar']),
			async getGoodsDetail(goods_id) {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/detail', {
					goods_id
				});
				if (res.meta.status !== 200) return uni.$showMsg();
				// 解决img底部空白间隙的问题
				res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g,
					'<img style="display:block;" ').replace(/webp/g, 'jpg');
				// 添加收藏状态信息
				const findRes = this.star.find(x => String(x.goods_id) === goods_id)
				if (!!findRes) {
					res.message.starInfo = findRes.starInfo
				} else {
					res.message.starInfo = {
						type: 'star',
						color: 'gray',
						text: '收藏',
						state: false
					}
				}
				this.goodsInfo = res.message;
			},
			preview(i) {
				uni.previewImage({
					current: i,
					urls: this.goodsInfo.pics.map(x => x.pics_big),
					loop: true
				})
			},
			// 底部导航栏事件
			onClick(e) {
				if (e.content.text === '购物车') {
					uni.switchTab({
						url: '/pages/cart/cart'
					})
				}
			},
			buttonClick(e) {
				if (e.content.text === '加入购物车') {
					// 组织商品的信息对象
					// 每个商品的信息对象，都包含如下 6 个属性：
					// { goods_id, goods_name, goods_price, goods_count, goods_small_logo, goods_state }
					const goods = {
						goods_id: this.goodsInfo.goods_id,
						goods_name: this.goodsInfo.goods_name,
						goods_price: this.goodsInfo.goods_price,
						goods_count: 1,
						goods_small_logo: this.goodsInfo.goods_small_logo,
						goods_state: true
					}
					// 调用addToCart方法
					this.addToCart(goods)
				}
			},
			// 加入收藏
			addGoodsToStar() {
				// 收藏状态改变
				if (!this.goodsInfo.starInfo.state) {
					this.goodsInfo.starInfo = {
						type: 'star-filled',
						color: 'orange',
						text: '已收藏',
						state: true
					}
				} else {
					this.goodsInfo.starInfo = {
						type: 'star',
						color: 'gray',
						text: '收藏',
						state: false
					}
				}
				// 组织商品的信息对象
				const goods = {
					goods_id: this.goodsInfo.goods_id,
					goods_name: this.goodsInfo.goods_name,
					goods_price: this.goodsInfo.goods_price,
					goods_count: 1,
					goods_small_logo: this.goodsInfo.goods_small_logo,
					goods_state: true,
					starInfo: this.goodsInfo.starInfo
				}
				if (this.goodsInfo.starInfo.state) {
					// 调用addToCart方法
					this.addtoStar(goods)
				} else {
					this.cancelStar(goods)
				}
			}
		}
	}
</script>

<style lang="scss">
	swiper {
		height: 750rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	// 商品信息区域的样式
	.goods-info-box {
		padding: 10px;
		padding-right: 0;

		.price {
			color: #c00000;
			font-size: 18px;
			margin: 10px 0;
		}

		.goods-info-body {
			display: flex;
			justify-content: space-between;

			.goods-name {
				font-size: 13px;
				padding-right: 10px;
			}

			// 收藏区域
			.favi {
				width: 120px;
				font-size: 12px;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				border-left: 1px solid #efefef;
				color: gray;
			}
		}

		// 运费
		.yf {
			margin: 10px 0;
			font-size: 12px;
			color: gray;
		}
	}

	.goods-detail-container {
		// 给页面外层的容器，添加 50px 的内padding，
		// 防止页面内容被底部的商品导航组件遮盖
		padding-bottom: 50px;
	}

	.goods_nav {
		// 为商品导航组件添加固定定位
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
	}
</style>
